package com.zhipu.balance.ui.home.calculate.rapeseed

import android.os.Bundle
import android.text.SpannableString
import android.view.LayoutInflater
import android.view.ViewGroup
import android.view.ViewTreeObserver.OnGlobalLayoutListener
import androidx.lifecycle.ViewModelProvider
import com.zhipu.balance.R
import com.zhipu.balance.common.Constants
import com.zhipu.balance.databinding.FragmentRapeseedDamageBinding
import com.zhipu.balance.mobile.dao.PersonnelDao
import com.zhipu.balance.mobile.event.Personnel
import com.zhipu.balance.mobile.event.RecordRapeseed
import com.zhipu.balance.mobile.event.RecordRice
import com.zhipu.balance.ui.BaseFragment
import com.zhipu.balance.ui.vm.home.calculate.rapeseed.RapeseedDamageViewModel
import com.zhipu.balance.utils.InitializeUtils
import org.angmarch.views.SpinnerTextFormatter

/**
 *  油菜籽热损伤粒
 */
class RapeseedDamageFragment(var mRecordRapeseed : RecordRapeseed) : BaseFragment<FragmentRapeseedDamageBinding>(), OnGlobalLayoutListener{

    /**
     * VM
     */
    private var mRapeseedDamageViewModel : RapeseedDamageViewModel? = null



    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        InitializeUtils.AppComponent()?.doInjectionMain(this)
        mRapeseedDamageViewModel = ViewModelProvider(this, viewModelFactory)[RapeseedDamageViewModel::class.java]
    }

    /**
     * 初始化根布局
     *
     * @return 布局layout的id
     */
    override fun initContentView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): Int {
        return R.layout.fragment_rapeseed_damage
    }

    override fun initView() {
        super.initView()
        binding.viewModel = mRapeseedDamageViewModel
        mRapeseedDamageViewModel?.mRecordRapeseed = mRecordRapeseed

    }



    /**
     * 重量
     */
    fun setWeighing(weighing : String?){
        if(null != mRapeseedDamageViewModel){
            mRapeseedDamageViewModel!!.weighing = weighing
        }

    }


    override fun initData() {
        super.initData()
        /**
         * 检测人员
         */
        var mPersonnelList = PersonnelDao.getInstance().allList
        if(null != mPersonnelList && mPersonnelList.size > 0){
            var mSpinnerTextFormatter = SpinnerTextFormatter<Personnel> { item ->
                mRapeseedDamageViewModel!!.mPersonnel = item
                SpannableString(item?.name)
            }
            binding.NiceSpinner.setSpinnerTextFormatter(mSpinnerTextFormatter)
            binding.NiceSpinner.setSelectedTextFormatter(mSpinnerTextFormatter)
            binding.NiceSpinner.attachDataSource(mPersonnelList)
            binding.NiceSpinner.setOnSpinnerItemSelectedListener { parent, view, position, id ->
                var mPersonnel : Personnel = binding.NiceSpinner.selectedItem as Personnel
                if(null != mPersonnel){
                    mRapeseedDamageViewModel!!.mPersonnel = mPersonnel
                }
            }
        }
    }


    /**
     * 更新稻谷实体
     */
    fun updateRecordRice(mRecordRapeseed : RecordRapeseed){
        mRapeseedDamageViewModel?.mRecordRapeseed = mRecordRapeseed
        mRapeseedDamageViewModel?.updateRecordRice()
    }

    /**
     * 释放更新页面
     */
    fun releaseRecordRice(){
        mRapeseedDamageViewModel?.releaseRecordRice()
    }





    /**
     * 计算
     */
    fun calculation(){
        binding.thermalDamageNumber1.clearFocus()
        binding.thermalDamageNumber2.clearFocus()
        /**
         * 计算
         */
        mRapeseedDamageViewModel!!.calculation()
    }

    /**
     * Callback method to be invoked when the global layout state or the visibility of views
     * within the view tree changes
     */
    override fun onGlobalLayout() {
        //比较根布局与当前布局的大小
        val heightDiff = binding.mRegisterScroll.rootView.height - binding.mRegisterScroll.height
        Constants.whetherInput = heightDiff <= 200
    }

}